﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;

namespace Steve
{
    public abstract class ContentTypeRepository : AuditRepositoryBase
    {
        const string ContentTypesKey = "ContentType";

        [Import]
        public ICacheService Cache { get; set; }
        [Import]
        public IConfiguration Config { get; set; }

        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        protected abstract IEnumerable<ContentType> GetContentTypesCore();

        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public IEnumerable<ContentType> GetContentTypes()
        {
            return Cache.GetValue<IEnumerable<ContentType>>(CacheType.ContentType, ContentTypesKey, Config.DataCacheTtl, () => GetContentTypesCore());
        }

        public ContentType GetContentType(string contentTypeName)
        {
            return GetContentTypes().Where(c => string.Equals(c.ContentTypeName, contentTypeName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        }        
    }
}
